package com.example.base.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.base.excetion.GlobalExcetion;
import com.example.base.system.domain.SysJobRecord;
import com.example.base.system.domain.vo.SysJobRecordVo;
import com.example.base.system.mapper.SysJobRecordMapper;
import com.example.base.system.service.SysJobRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author X.I.O
 * @since 2024-02-20
 */
@Service
@Transactional
@CacheConfig(cacheNames = "SysJobRecordServiceImpl")
public class SysJobRecordServiceImpl extends ServiceImpl<SysJobRecordMapper, SysJobRecord> implements SysJobRecordService {

    @Cacheable(cacheNames = "sysjobrecord",key = "#id")
    @Override
    public SysJobRecord getById(Serializable id){
        return super.getById(id);
    }

    @CacheEvict(cacheNames = "sysjobrecord", key = "#id")
    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @CacheEvict(cacheNames = "sysjobrecord", key = "#sysjobrecord.id")
    @Override
    public boolean updateById(SysJobRecord sysjobrecord) {
        return super.updateById(sysjobrecord);
    }

    @CachePut(cacheNames = "sysjobrecord", key = "#sysjobrecord.id")
    public SysJobRecord saveSysJobRecord(SysJobRecord sysjobrecord) throws GlobalExcetion {
        super.save(sysjobrecord);
        return sysjobrecord;
    }

    @CacheEvict(cacheNames = "sysjobrecord", key = "#sysjobrecord.id")
    public SysJobRecord updateSysJobRecordById(SysJobRecord sysjobrecord) throws GlobalExcetion{
        super.updateById(sysjobrecord);
        return sysjobrecord;
    }


    public SysJobRecord saveOrUpdateSysJobRecord(SysJobRecordVo sysjobrecordVo) throws GlobalExcetion{
        SysJobRecord sysjobrecord=new SysJobRecord();
        BeanUtils.copyProperties(sysjobrecordVo,sysjobrecord);
        if(sysjobrecordVo.getId()!=null){
            SysJobRecord sysjobrecordEntity=super.baseMapper.selectById(sysjobrecordVo.getId());
            if(sysjobrecordEntity==null){
                throw new GlobalExcetion(1,"数据不存在");
            }
            super.baseMapper.updateById(sysjobrecord);
        }else{
            super.baseMapper.insert(sysjobrecord);
        }
        return sysjobrecord;
    }

    @Override
    public IPage<SysJobRecord> page(Serializable id, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<SysJobRecord> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysJobRecord::getJobId,id);
        lambdaQueryWrapper.orderByDesc(SysJobRecord::getCreatetime);
        IPage<SysJobRecord> page=super.baseMapper.selectPage(new Page(pageNum,pageSize),lambdaQueryWrapper);
        return page;
    }
}
